package com.share.example.spider.schedule;

import com.share.example.spider.pipeline.FundPipeline;
import com.share.example.spider.processor.FundProcessor;
import com.share.example.spider.service.IFundService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import javax.annotation.PostConstruct;
import java.util.List;

@Slf4j
@Component
public class FundSchedule {

    @Autowired
    private FundPipeline fundPipeline;

    @Autowired
    private IFundService iFundService;

    @PostConstruct
    @Scheduled(cron = "0 0/10 * * * ?")
    public void pullStock() {
        log.info("开始异步提交任务");
        Spider spider = Spider.create(new FundProcessor());/*这里只能用new*/

        //股票列表
        List<String> list = iFundService.listAll(null);
        for (String fundCode : list) {
            String url = String.format("http://fund.eastmoney.com/%s.html", fundCode);
            spider.addUrl(url);
        }

        spider.addPipeline(fundPipeline);
        spider.thread(3);
        spider.setExitWhenComplete(true);
        spider.start();
        log.info("异步任务提交结束!");
    }

}
